package com.xnfh.equipment.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.common.bean.EquipmentToken
import com.xnfh.common.bean.GuanGaiBean
import com.xnfh.common.bean.WarningBean
import com.xnfh.equipment.R
import com.xnfh.equipment.adapter.SystemLogAdapter
import com.xnfh.equipment.adapter.SystemLogIrrigatteAdapter
import com.xnfh.equipment.databinding.FragmentEquipmentIrrigateLayoutBinding
import com.xnfh.equipment.viewmodel.EquipmentLogViewModel
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.network.constant.SFJ_API_TOKEN
import com.xnfh.network.constant.SFJ_TOKEN_V2

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/12/27 14:14
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/27 14:14
 * @UpdateRemark: 更新说明
 */
class EquipmentIrrigateFragment: BaseFragment<FragmentEquipmentIrrigateLayoutBinding>() {
    override fun getLayoutID(): Int = R.layout.fragment_equipment_irrigate_layout

    companion object {
        fun getInstance(bundle: Bundle? = null): EquipmentIrrigateFragment {
            val plantOptionFragment = EquipmentIrrigateFragment()
            if (bundle != null) {
                plantOptionFragment.arguments = bundle
            }
            return plantOptionFragment
        }
    }

    private var viewModel : EquipmentLogViewModel? = null
    private var page = 1
    private var gateWayId: String? = null
    private var adapter : SystemLogIrrigatteAdapter? = null

    override fun initView(view: View, savedInstanceState: Bundle?) {
        gateWayId = arguments?.getString("id")
        mBind.logIrrigateRecycle.layoutManager = LinearLayoutManager(requireContext())
        adapter = SystemLogIrrigatteAdapter()
        mBind.logIrrigateRecycle.adapter = adapter
        mBind.logIrrigateRefresh.autoRefresh()
        mBind.logIrrigateRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel?.requestKcTokenV2()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel?.requestKcTokenV2()
            }
        })
    }

    override fun observe() {
        viewModel = EquipmentLogViewModel()
        viewModel?.tokenV2?.observe(this,object : BaseStateObserver<EquipmentToken>(){
            override fun getRespDataSuccess(it: EquipmentToken) {
                super.getRespDataSuccess(it)
                SFJ_API_TOKEN = it.apiToken
                SFJ_TOKEN_V2 = it.TokenV2
                mBind.logIrrigateRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
                viewModel?.requestGuanGaiList(gateWayId, page)
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.logIrrigateRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })
        viewModel?.guanGaiBean?.observe(this,object : BaseStateObserver<GuanGaiBean>(){
            override fun getRespDataSuccess(it: GuanGaiBean) {
                super.getRespDataSuccess(it)
                mBind.logIrrigateRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
                if(page == 1 && it.Data.isEmpty()){
                    adapter?.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                    return
                }
                if(page == 1) {
                    adapter?.setNewInstance(it.Data.toMutableList())
                }else{
                    adapter?.addData(it.Data)
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.logIrrigateRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })
    }
}